<?php

class CategoryController extends Controller {

    /**
     *  list category
     */
    public function actionIndex() {
        $category = Category::model()->findByPk(1);
        $descendants = $category->descendants()->findAll();
        $this->render('index');
    }

    /**
     * 
     */
    public function actionEdit($id = null) {
        if ($id != null && $id != 1) {
            $model = Category::model()->findByPk($id);
            if ($model != null) {
                if (isset($_POST['Category'])) {
                    $parent = Category::model()->findByPk($_POST['Category']['parents']);
                    if ($parent != null) {
                        $model->attributes = $_POST['Category'];
                        $image = CUploadedFile::getInstance($model, 'image');
                        if ($image != null) {
                            $newName = time() . "." . $image->extensionName;
                            $model->image = $newName;
                        }
                        if ($model->save() && $model->moveAsFirst($parent)) {
                            if($image != null && isset($newName)){
                                 $image->saveAs(PATH_IMAGE_CATEGORY . $newName);
                            }
                            Yii::app()->user->setFlash('success', "Danh mục sản phẩm đã được lưu");
                            $this->redirect(array('category/index'));
                        }
                    }
                }
                $listCategory = Category::getListCategory(1, $id);
                $this->render('add', array('model' => $model, 'listCategory' => $listCategory, 'id' => $id));
            } else {
                throw new CHttpException(404, 'The specified post cannot be found.');
            }
        } else {
            throw new CHttpException(404, 'The specified post cannot be found.');
        }
    }

    /**
     * 
     */
    public function actionAdd() {
        $model = new Category();
        $listCategory = Category::getListCategory(1);
        if (isset($_POST['Category'])) {
            $model->attributes = $_POST['Category'];
            $parent = Category::model()->findByPk($_POST['Category']['parents']);
            if ($parent != null) {
                $image = CUploadedFile::getInstance($model, 'image');
                if ($image != null) {
                    $newName = time() . "." . $image->extensionName;
                    $model->image = $newName;
                }
                $result = $model->appendTo($parent);
                if ($result) {
                    if($image != null && isset($newName)){
                          $image->saveAs(PATH_IMAGE_CATEGORY . $newName);
                    }
                    Yii::app()->user->setFlash('success', "Danh mục sản phẩm đã được lưu");
                    $this->redirect(array('category/index'));
                }
            }
        }
        $this->render('add', array('model' => $model, 'listCategory' => $listCategory));
    }
    
    /**
     * 
     */
    public function actionDelete($id=null){
        if ($id != null && $id != 1) {
            $model = Category::model()->findByPk($id);
            if ($model != null) {
                $categories = $model->getArrayIdChildrenCategory();
                $criteria = new CDbCriteria();
                $criteria->addInCondition('category_id', $categories);
                Product::model()->updateAll(array('category_id' => 0), $criteria);
                $model->deleteNode();
                Yii::app()->user->setFlash('success', "Danh mục sản phẩm đã được xóa");
                $this->redirect(array('category/index'));
            } else {
                throw new CHttpException(404, 'The specified post cannot be found.');
            }
        } else {
            throw new CHttpException(404, 'The specified post cannot be found.');
        }
    }

}